home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / mcomm551.zip / COMM.HPP < prev    next >
C/C++ Source or Header  |  1991-11-28  |  18KB  |  382 lines

  1.  
  2. /////////////////////////////////////////////////////////////////////////////
  3. //                                                                         //
  4. //  COMM.HPP -- header file for C++ programs using MCOMM async routines.   //
  5. //    Zortech C++ 2.0, Turbo C++ 1.00 version.                             //
  6. //                                                                         //
  7. //    Mike Dumdei,  6 Holly Lane,  Texarkana TX  75503    (c) 1989,1990    //
  8. //                                                                         //
  9. /////////////////////////////////////////////////////////////////////////////
  10.  
  11. #if !defined(COMM_HPP)
  12. #define COMM_HPP
  13.  
  14. #ifndef MCOMM
  15.   #define _C_ cdecl
  16.   #define _V_ volatile
  17.   #define _F_ far
  18.   #if (__TURBOC__ && __HUGE__)
  19.     #define _N_
  20.   #else
  21.     #define _N_ near
  22.   #endif
  23.   #define MCOMM
  24. #endif
  25.  
  26. //////////////////////////////////////////////
  27. //  Return codes                            //
  28. //////////////////////////////////////////////
  29. #ifndef __ZTC__
  30.   #define   R_OK           0x0000
  31. #endif
  32. #define     R_NOMEM        0x0001
  33. #define     R_BAUDERR      0x0002
  34. #define     R_PARITYERR    0x0004
  35. #define     R_DTABITERR    0x0008
  36. #define     R_STPBITERR    0x0010
  37. #define     R_IRQUSED      0x0020
  38. #define     R_VCTRUSED     0x0040
  39. #define     R_NOPORT       0x0080
  40. #define     R_PORTUSED     0x0100
  41. #define     R_UARTERR      0x0200
  42.  
  43. #define     R_TXERR        (-1)
  44.  
  45. //////////////////////////////////////////////
  46. //  Interrupt bit masks                     //
  47. //////////////////////////////////////////////
  48. #define     IRQ3           0x08
  49. #define     IRQ4           0x10
  50.  
  51. //////////////////////////////////////////////
  52. //  Vector numbers                          //
  53. //////////////////////////////////////////////
  54. #define     VCTR3          0x0b
  55. #define     VCTR4          0x0c
  56.  
  57. //////////////////////////////////////////////
  58. //  Standard Comm Ports                     //
  59. //   Note - due to the ability to overload  //
  60. //   the open call, COM1 and COM2 are def-  //
  61. //   ined differently in the C++ version.   //
  62. //////////////////////////////////////////////
  63. #define     COM1        0
  64. #define     COM2        1
  65.  
  66. //////////////////////////////////////////////
  67. //  Defines for async_regs function         //
  68. //////////////////////////////////////////////
  69. #define     RXreg       0
  70. #define     TXreg       0
  71. #define     IERreg      1
  72. #define     IIDreg      2
  73. #define     FCRreg      2
  74. #define     LCRreg      3
  75. #define     MCRreg      4
  76. #define     LSRreg      5
  77. #define     MSRreg      6
  78. #define     LObaud      0
  79. #define     HIbaud      1
  80.  
  81. //////////////////////////////////////////////
  82. //  Miscellaneous bit masks                 //
  83. //////////////////////////////////////////////
  84. #define     B_DTR        0x0001
  85. #define     B_RTS        0x0002
  86.  
  87. /////////////////////////////////////////////////////////////////////////
  88. //  Status bit masks                                                   //
  89. /////////////////////////////////////////////////////////////////////////
  90.   //  S T A T 2  //
  91. #define     B_XUSE       0x0001   // using XON/XOFF protocol bit
  92. #define     B_XRXD       0x0002   // XOFF received bit
  93. #define     B_XSENT      0x0004   // XOFF sent bit
  94. #define     B_TXEMPTY    0x0008   // nothing to transmit bit
  95. #define     B_CTS        0x0010   // clear to send bit
  96. #define     B_DSR        0x0020   // data set ready bit
  97. #define     B_FLOWHALT   0x0040   // flow halt indicator bit
  98. #define     B_MNTR_CD    0x0080   // monitor CD bit
  99.  
  100.   //  S T A T 1  //
  101. #define     B_RXOVF      0x0100   // rx buffer overflow indic bit
  102. #define     B_OVERRUN    0x0200   // char overrun error bit
  103. #define     B_PARITY     0x0400   // parity error bit
  104. #define     B_FRAMING    0x0800   // framing error bit
  105. #define     B_BREAK      0x1000   // line break signal detected
  106. #define     B_THRE       0x2000   // used in interrupt hdlr (always 0)
  107. #define     B_RXEMPTY    0x4000   // rx buffer empty bit
  108. #define     B_CD         0x8000   // carrier detect bit mask
  109.  
  110.   //  S T A T 3  //
  111. #define     B_IGNERR     0x0001   // discard chars with par, frm'g errs
  112. #define     B_16550      0x0002   // 16550 detected
  113. #define     B_FIFO       0x0004   // 16550 FIFOs enabled
  114. #define     B_ORGFIFO    0x0008   // FIFOs were on when port was opened
  115. #define     B_FARBUFFER  0x0010   // using FAR ring buffers
  116. #define     B_XUSET      0x0020   // using XON/XOFF on txmt side bit
  117. #define     B_XUSER      0x0040   // using XON/XOFF on recv side bit
  118. #define     B_XONANY     0x0080   // accept any char for an XON
  119.  
  120.  
  121. /////////////////////////////////////////////////////////////////////////
  122. //                                                                     //
  123. //  A s y n c P o r t   C l a s s                                      //
  124. //                                                                     //
  125. //                    * * *  W A R N I N G  * * *                      //
  126. //     This class must have the port variables byte aligned.  As       //
  127. //     written, ZTC 2.0 will byte align this structure without the     //
  128. //     need to direct it to pack structures.  If you are using         //
  129. //     a different implementation of C++ be sure this data is          //
  130. //     byte aligned by your compiler.                                  //
  131. /////////////////////////////////////////////////////////////////////////
  132.  
  133. class AsyncPort
  134. {
  135.       // port variables
  136.   private:
  137.     int ComBase;          // base port address of for comm port
  138.     char IRQMask;         // mask for IRQ used by this port
  139.     char Vector;          // interrupt vector used by this port
  140.     char BPDSstr[10];     // current baud,parity,data,stop parms
  141.     int RxSize;           // size of receive bufr
  142.     int TxSize;           // size of transmit bufr
  143.     int RingSeg;          // segment of rx/tx bufrs (leave 0 for NEAR)
  144.     int RingOfst;         // offset of rx/tx bufrs, alias TxTop
  145.     int RxTop;            // ptr to begin of rx bufr, alias TxBtm
  146.     int RxBtm;            // ptr to end of rx bufr (end + 1)
  147.     int _V_ TxIn;         // ptr to where next 'put' goes
  148.     int _V_ TxOut;        // ptr to where next tx'd comes from
  149.     int _V_ TxFree;       // remaining tx bufr space
  150.     int _V_ RxIn;         // pointer to where next rx'd goes
  151.     int _V_ RxOut;        // ptr to where next 'get' comes from
  152.     int _V_ RxFree;       // remaining bytes in rx bufr space
  153.     int BaudDvsr;         // baud rate divisor
  154.     int XoffTrip;         // send XOFF when this many bytes left in rxbuf
  155.     int XonTrip;          // send XON when this many bytes left in rxbuf
  156.     int OldVctrOfst;      // OFST address of original interrupt vector
  157.     int OldVctrSeg;       // SEG address of original interrupt vector
  158.     int OldDvsr;          // original value of Baud Divisor Regs
  159.     char _V_ Stat2;       // secondary status byte
  160.     char _V_ Stat1;       // primary status byte
  161.     char _V_ TxStat;      // determines when tx_intrpts need turned on
  162.     char MSRMask;         // slcts bits in MSR montrd for flow cntrl
  163.     char _V_ MSRVal;      // present value of modem status register
  164.     char _V_ Stat3;       // another flag, used for XONANY char
  165.     char PDSinfo;         // parity, # dta bits, # stop bits
  166.     char StripMask;       // mask for high bit stripping
  167.     char XTxRptInit;      // initialization value for XTxRpt below
  168.     char _V_ XTxRpt;      // countdown val befr repeat tx of XOFF
  169.     char _V_ TxImmedChar; // char to be tx'd 1st opportunity
  170.     char HdlrUsed;        // assigned interrupt handler for this port
  171.     char Old8259Msk;      // original 8259 interrupt mask
  172.     char OldLCR;          // original Line Control Reg value
  173.     char OldMCR;          // original Modem Cntrl Reg value
  174.     char OldIER;          // original Interrupt Enable Reg value
  175.     int TxByteCnt;        // number bytes to send per tx interrupt
  176.     char FCRVal;          // FIFO control register data byte
  177.     char IERVal;          // interrupt enable register value
  178.     char _V_ MCRVal;      // modem control register value
  179.  
  180.       // declaration of default ring buffer variables
  181.     static int DefRxBufSz;      // default receive ring buffer size
  182.     static int DefTxBufSz;      // default transmit buffer size
  183.     static int DefMemType;      // default to NEAR or FAR memory flag
  184.  
  185.   public:
  186.     AsyncPort();                    // constructors
  187.     AsyncPort(int RxBufSz, int TxBufSz, int UseFarMem = 0);
  188.  
  189.     ~AsyncPort();                   // destructor
  190.  
  191.                                     // allocate ring buffers function
  192.     int AllocBuffers(int RxBufSz, int TxBufSz, int UseFarMem = 0);
  193.     int FreeBuffers();              // free ring buffers function
  194.                                     // reset default buffers function
  195.     static void SetDefaultBuffers(int RxDefSz, int TxDefSz, int DefMem = 0);
  196.  
  197.                                     // open with full port definition
  198.     int Open(int ComBase, int IRQNbr, int InterruptVctr, char *Params = "");
  199.     int Open(int ComN, char *Params = ""); // open COM1 or COM2
  200.  
  201.     int Close();                    // close & leave DTR/RTS in pre-open state
  202.     int Close(int DTR, int RTS);    // close with option to force DTR/RTS
  203.  
  204.     void Release();                 // disable interrupt operation
  205.     void Restart();                 // resume interrupt mode of operation
  206.  
  207.     void PortAddress(int BaseAdrs); // set port base address
  208.     int PortAddress();              // get port base address
  209.  
  210.     int Tx(char Ch);                // transmit character
  211.     int Tx(char *String);           // transmit string
  212.     int Tx(char *Block, int NbrBytes); // transmit block
  213.  
  214.     int Rx();                       // receive character
  215.     int Rx(char *Block, int MaxNbrBytes); // receive block until maxbytes
  216.                                     // rx blk until delim or maxbytes
  217.     int Rx(char *Block, int MaxNbrBytes, char Delim, int IncludeDelim = 1);
  218.  
  219.     int Peek(int Offset = 0);       // peek receive buffer (index opt)
  220.  
  221.     int Status(int Mask = 0xffff);  // get port status (Stat1/Stat2)
  222.     void Reset();                   // reset parity,framing,char/bufr overrun
  223.     char Error(int Mask = 0x0f);    // test for parity,frame,char/bufr overrun
  224.  
  225.     int Params(char *Params);       // set port parameters
  226.     char *Params();                 // get port parameters
  227.  
  228.     void RxFlush();                 // flush receive buffer
  229.     void TxFlush();                 // flush transmit buffer
  230.  
  231.     void HardFlow(int Mask);        // set/reset hardware flow options
  232.  
  233.     void XFlow(int Mask);           // set/reset XON/XOFF flow control
  234.     char XFlow();                   // read XON/XOFF flow settings (bitmapped)
  235.                                     // set XON/XOFF trip levels
  236.     int XTrip(int XoffLevel, int XonLevel, int RepeatLevel);
  237.     void XoffReset();               // forced reset of XOFF received condition
  238.     void XoffSet();                 // simulate XOFF received
  239.     char XoffRxd();                 // test for XOFF received
  240.     char XoffTxd();                 // test for XOFF transmitted
  241.  
  242.     void Dtr(int Enable);           // set/reset DTR line
  243.     void Rts(int Enable);           // set/reset RTS line
  244.     char Msr();                     // get value of modem status register
  245.     char Cts();                     // get status of CTS line
  246.     char Dsr();                     // get status of DSR line
  247.     char Carrier();                 // get status of carrier detect line
  248.  
  249.     void Break(int Enable);         // set or reset breaking condition
  250.     char Break();                   // check if break signal received
  251.  
  252.     char Regs(int UARTreg, int Value = -1); // raw R/W of UART regs
  253.  
  254.     int RxLevel();                  // get number bytes in receive buffer
  255.     int TxAvailable();              // get bytes available in transmit buffer
  256.     char TxEmpty();                 // test for transmit buffer empty
  257.  
  258.     void RxMask(char Mask);         // set strip mask for incoming chars
  259.     void FlushBadChars(int Flag);   // set handling for chars with errs
  260.     char FlushBadChars();           // get setting chars with errs
  261. };
  262.  
  263. /////////////////////////////////////////////////////////////////////////
  264. //  E X T E R N A L   A S M   F U N C T I O N S                        //
  265. /////////////////////////////////////////////////////////////////////////
  266.   extern "C" {
  267. int async_open(AsyncPort *, int, int, int, char *); // open async port
  268. int async_close(AsyncPort *);              // close async port
  269. int async_tx(AsyncPort *, char);           // transmit character
  270. int async_txblk(AsyncPort *, char *, int); // transmit a block
  271. int async_rx(AsyncPort *);                 // receive a character
  272. int async_rxblk(AsyncPort *, char *, int); // receive a block
  273. int async_rxblkch(AsyncPort *, char *, int, char, int); // rx blk till char
  274. int async_stat(AsyncPort *, int);          // get stat1/stat2 bytes
  275. int async_setbpds(AsyncPort *, char *);    // change line parameters
  276. void async_rxflush(AsyncPort *);           // flush the receive buffer
  277. void async_txflush(AsyncPort *);           // flush the transmit buffer
  278. void async_msrflow(AsyncPort *, int);      // set MSR related flw cntrl
  279. void async_xflow(AsyncPort *, int);        // XON/OFF protocl on/off
  280. void async_xoffclr(AsyncPort *);           // manually clr XOFF rx'd
  281. void async_xoffset(AsyncPort *);           // manually set XOFF rx'd
  282. void async_dtr(AsyncPort *, int);          // DTR on/off
  283. void async_rts(AsyncPort *, int);          // RTS on/off
  284. void async_sndbrk(AsyncPort *, int);       // Set/clr tx modem brk sgnl
  285. void async_stop(AsyncPort *);              // halts interrupt operation
  286. void async_restart(AsyncPort *);           // re-init an already open port
  287. int async_peek(AsyncPort *, int);          // peek indexed char in rx buffer
  288. int async_regs(AsyncPort *, int, int);     // direct read/write of port regs
  289. void async_FIFOrxlvl(AsyncPort *, int);    // set rx FIFO trigger level
  290. void async_FIFOtxlvl(AsyncPort *, int);    // set max sent per tx intrpt
  291.   }
  292.  
  293.  
  294. /////////////////////////////////////////////////////////////////////////
  295. //  D E F I N I T I O N    O F   I N L I N E   F U N C T I O N S       //
  296. /////////////////////////////////////////////////////////////////////////
  297.  
  298. inline void AsyncPort::SetDefaultBuffers(int DefRxSz,int DefTxSz,int DefMem)
  299.  { DefRxBufSz = DefRxSz, DefTxBufSz = DefTxSz, DefMemType = DefMem; }
  300. inline void AsyncPort::PortAddress(int BaseAdrs)
  301.  { ComBase = BaseAdrs; }
  302. inline int AsyncPort::PortAddress()
  303.  { return ComBase; }
  304. inline void AsyncPort::Release()
  305.  { async_stop(this); }
  306. inline void AsyncPort::Restart()
  307.  { async_restart(this); }
  308. inline int AsyncPort::Tx(char Ch)
  309.  { return (async_tx(this, Ch)); }
  310. inline int AsyncPort::Tx(char *Block, int NbrBytes)
  311.  { return (async_txblk(this, Block, NbrBytes)); }
  312. inline int AsyncPort::Rx()
  313.  { return (async_rx(this)); }
  314. inline int AsyncPort::Rx(char *Block, int MaxNbrBytes)
  315.  { return (async_rxblk(this, Block, MaxNbrBytes)); }
  316. inline int AsyncPort::Rx(char *Block, int MaxNbrBytes, char Delim,
  317.  int IncludeDelim)
  318.  { return (async_rxblkch(this, Block, MaxNbrBytes, Delim, IncludeDelim)); }
  319. inline int AsyncPort::Peek(int Offset)
  320.  { return (async_peek(this, Offset)); }
  321. inline int AsyncPort::Status(int Mask)
  322.  { return (async_stat(this, Mask)); }
  323. inline void AsyncPort::Reset()
  324.  { Stat1 &= '\xe0'; }
  325. inline char AsyncPort::Error(int Mask)
  326.  { return (Stat1 & Mask); }
  327. inline int AsyncPort::Params(char *Params)
  328.  { return (async_setbpds(this, Params)); }
  329. inline char * AsyncPort::Params()
  330.  { return BPDSstr; }
  331. inline void AsyncPort::RxFlush()
  332.  { async_rxflush(this); }
  333. inline void AsyncPort::TxFlush()
  334.  { async_txflush(this); }
  335. inline void AsyncPort::HardFlow(int Mask)
  336.  { async_msrflow(this, Mask); }
  337. inline void AsyncPort::XFlow(int Mask)
  338.  { async_xflow(this, Mask); }
  339. inline char AsyncPort::XFlow()
  340.  { return (Stat3 & '\xe0'); }
  341. inline void AsyncPort::XoffReset()
  342.  { async_xoffclr(this); }
  343. inline void AsyncPort::XoffSet()
  344.  { async_xoffset(this); }
  345. inline char AsyncPort::XoffRxd()
  346.  { return (TxStat & '\x02'); }
  347. inline char AsyncPort::XoffTxd()
  348.  { return (Stat2 & '\x04'); }
  349. inline void AsyncPort::Dtr(int Enable)
  350.  { async_dtr(this, Enable); }
  351. inline void AsyncPort::Rts(int Enable)
  352.  { async_dtr(this, Enable); }
  353. inline char AsyncPort::Msr()
  354.  { return MSRVal; }
  355. inline char AsyncPort::Cts()
  356.  { return (MSRVal & '\x10'); }
  357. inline char AsyncPort::Dsr()
  358.  { return (MSRVal & '\x20'); }
  359. inline char AsyncPort::Carrier()
  360.  { return (MSRVal & '\x80'); }
  361. inline void AsyncPort::Break(int Enable)
  362.  { async_sndbrk(this, Enable); }
  363. inline char AsyncPort::Break()
  364.  { return (Stat1 & '\x10'); }
  365. inline char AsyncPort::Regs(int UARTreg, int Value)
  366.  { return async_regs(this, UARTreg, Value); }
  367. inline int AsyncPort::RxLevel()
  368.  { return (RxSize - RxFree); }
  369. inline int AsyncPort::TxAvailable()
  370.  { return (TxFree); }
  371. inline char AsyncPort::TxEmpty()
  372.  { return (TxStat & '\x08'); }
  373. inline void AsyncPort::RxMask(char Mask)
  374.  { StripMask = Mask; }
  375. inline void AsyncPort::FlushBadChars(int Flag)
  376.  { (Flag) ? (Stat3 |= 1) : (Stat3 &= '\xfe'); }
  377. inline char AsyncPort::FlushBadChars()
  378.  { return (Stat3 & 1); }
  379.      
  380. #endif          // COMM_HPP
  381.  
  382.